package com.iafme.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.LearnReqDTO;
import com.iafme.dto.res.LearnResDTO;
import com.iafme.model.Learn;
import com.iafme.service.LearnService;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.util.JsoupUtil;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/22 16:42
 */
@RestController
@RequestMapping(value = "/learn")
@AllArgsConstructor
@Slf4j
public class LearnController implements BaseCommonInterface<LearnReqDTO, LearnResDTO> {

    private static final Logger log = LoggerFactory.getLogger(LearnController.class);
    private final LearnService learnService;

    private final JsoupUtil jsoupUtil;

    @Override
    public Result<String> save(@Valid LearnReqDTO learnReqDTO) {
        Learn learn = Convert.convert(Learn.class, learnReqDTO);
        String html = jsoupUtil.replaceOssImage(learn.getContext());
        learn.setContext(html);
        learnService.save(learn);
        return Result.successResult();
    }

    @Override
    public Result<String> update(@Valid LearnReqDTO learnReqDTO) {
        Learn learn = Convert.convert(Learn.class, learnReqDTO);
        String html = jsoupUtil.replaceOssImage(learn.getContext());
        learn.setContext(html);
        learnService.updateById(learn);
        return Result.successResult();
    }

    @Override
    public Result<String> del(Integer integer) {
        return null;
    }

    @Override
    public Result<Page<LearnResDTO>> lists(LearnReqDTO learnReqDTO) {
        return null;
    }

    @Override
    public Result<LearnResDTO> findById(Integer integer) {
        return null;
    }

    @Override
    public Result<String> batchDel(List<LearnResDTO> list) {
        return null;
    }

    @GetMapping(value = "/get/{typeId}")
    public LearnResDTO get(@PathVariable Integer typeId){
        Learn learn = learnService.getOne(Wrappers.lambdaQuery(Learn.class).eq(Learn::getType, typeId));
        return Convert.convert(LearnResDTO.class,learn);
    }
}
